package com.yuanrui.circle.server.rpc;

import com.yuanrui.auth.api.UserFeignService;
import com.yuanrui.auth.entity.AuthUserDTO;
import com.yuanrui.auth.entity.Result;
import com.yuanrui.circle.server.entity.UserInfo;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户rpc
 */
@Component
public class UserRpc {
    @Resource
    private UserFeignService userFeignService;

    public UserInfo getUserInfo(String userName) {
        AuthUserDTO AuthUserDTO = new AuthUserDTO();
        AuthUserDTO.setUserName(userName);
        Result<AuthUserDTO> userInfo = userFeignService.getUserInfo(AuthUserDTO);
        if (userInfo.getSuccess()) {
            UserInfo userInfo1 = new UserInfo();
            userInfo1.setUserName(userInfo.getData().getUserName());
            userInfo1.setNickName(userInfo.getData().getNickName());
            userInfo1.setAvatar(userInfo.getData().getAvatar());
            return userInfo1;
        }else {
            return null;
        }
    }
    public Map<String, UserInfo> batchGetUserInfo(List<String> userNameList) {
        if (CollectionUtils.isEmpty(userNameList)) {
            return Collections.emptyMap();
        }
        Result<List<AuthUserDTO>> listResult = userFeignService.getUserInfo(userNameList);
        if (Objects.isNull(listResult) || !listResult.getSuccess() || Objects.isNull(listResult.getData())) {
            return Collections.emptyMap();
        }
        Map<String, UserInfo> result = new HashMap<>();
        for (AuthUserDTO data : listResult.getData()) {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserName(data.getUserName());
            userInfo.setNickName(data.getNickName());
            userInfo.setAvatar(data.getAvatar());
            result.put(userInfo.getUserName(), userInfo);
        }
        return result;
    }
}
